package com.smt.modules.bus.service.impl;

import com.smt.common.enums.Code;
import com.smt.common.utils.*;
import com.smt.modules.bus.entity.BillEntity;
import com.smt.modules.bus.entity.GradeEntity;
import com.smt.modules.bus.entity.MemberUserEntity;
import com.smt.modules.bus.service.BillService;
import com.smt.modules.bus.service.MemberUserService;
import com.smt.modules.bus.vo.CompanyVo;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import com.smt.modules.bus.dao.CompanyDao;
import com.smt.modules.bus.entity.CompanyEntity;
import com.smt.modules.bus.service.CompanyService;


@Service("companyService")
public class CompanyServiceImpl extends ServiceImpl<CompanyDao, CompanyEntity> implements CompanyService {

    @Autowired
    private BillUtils billUtils;

    @Autowired
    private MemberUserService memberUserService;

    @Autowired
    private BillService billService;

    /**
     * 获取用户的公司列表信息
     *
     * @param memberUserEntity
     * @return
     */
    @Override
    public R queryCompanyList(MemberUserEntity memberUserEntity) {
        Long userId = memberUserEntity.getUserId();
        List<CompanyEntity> companyEntities = selectList(new EntityWrapper<CompanyEntity>()
                .eq("member_id", userId));
        if(companyEntities.size() != 0) {
            return R.ok().put("company",companyEntities);
        }else{
            return R.error(EnumToKeyUtil.toInt(Code.DELETE_DATA_ERROR),"您还未创建公司");
        }
    }

    /**
     * 创建公司信息
     *
     * @param company
     * @param memberUserEntity
     * @return
     */
    @Override
    public R create(CompanyVo company, MemberUserEntity memberUserEntity) {
        Long userId = memberUserEntity.getUserId();
        List<CompanyEntity> companyEntities = selectList(new EntityWrapper<CompanyEntity>()
                .eq("member_id", userId));
        if(companyEntities.size() != 0){
            return R.error(EnumToKeyUtil.toInt(Code.QUERY_DATA_ERROR),"您创建公司已经存在");
        }else {
            CompanyEntity companyEntity = new CompanyEntity();
            CompanyEntity companyE = company.copyPropertiesTo(companyEntity);
            companyE.setMemberId(userId);
            companyEntity.setCreateTime(DateTime.now().toDate());
            companyEntity.setUpdateTime(DateTime.now().toDate());
            boolean insert = insert(companyE);
            if(insert){
                memberUserEntity.setCompanyId(companyE.getId());
                memberUserService.updateById(memberUserEntity);
            }
            //这里的简介和营业执照奖励发放
            String brief = companyE.getBrief();
            String imageBusinessLicense = companyE.getImageBusinessLicense();
            if(brief != null && brief != ""){
                BillEntity billEntity = billService.selectOne(new EntityWrapper<BillEntity>()
                        .eq("type", 1)
                        .eq("bus_business_id", userId)
                        .eq("expenditure", 1)
                        .eq("bill_title", Constant.TASK_3));
                billEntity.setIsReward(1);
                billEntity.setTransactionName(memberUserEntity.getUsername());
                billEntity.setTransactionTime(DateTime.now().toDate());
                billService.updateById(billEntity);
            }
            if(imageBusinessLicense != null && imageBusinessLicense != ""){
                BillEntity billEntity = new BillEntity();
                billEntity.setBillTitle(Constant.TASK_5);
                billUtils.addBills(billEntity,memberUserEntity,null);
            }
            return insert?R.ok("创建公司成功"):R.error(EnumToKeyUtil.toInt(Code.SYSTEM_EXCEPTION),"创建公司失败");
        }
    }

}
